﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;

namespace PPAP.Web.Master
{
    public partial class PPAPNewsEdit : PageBase
    {
        PPAPNewsBO mbo = new PPAPNewsBO();
        LogBO lbo = new LogBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    if (Request.QueryString["id"].ToString() == "1")
                    {
                        type.Visible = false;
                        tr.Visible = false;
                    }
                    PPAP.Model.PPAPNews mail = mbo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
                    txtTitle.Text = mail.TITLE;
                    content1.InnerText = mail.CONTENT;
                    //RadioButton1.Checked = mail.TYPE == 1 ? true : false;
                    //RadioButton2.Checked = mail.TYPE == 2 ? true : false;
                    if (mail.TYPE == 1)
                        RadioButton1.Checked = true;
                    else if (mail.TYPE == 2)
                        RadioButton2.Checked = true;
                    else
                        RadioButton3.Checked = true;
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            int num = 0;
            PPAP.Model.PPAPNews mail = null;
            if (Request.QueryString["id"] != null)
                mail = mbo.GetModel(int.Parse(Request.QueryString["id"].ToString()));
            else
                mail = new Model.PPAPNews();
            mail.TITLE = CheckWord(txtTitle.Text);
            mail.CREATETIME = DateTime.Now;
            mail.CREATEDBY = CurrentUser.Id;
            mail.CONTENT = CheckWord(content1.InnerText);
            mail.TYPE = RadioButton1.Checked ? 1 : 2;
            if (RadioButton1.Checked)
                mail.TYPE = 1;
            else if (RadioButton2.Checked)
                mail.TYPE = 2;
            else
                mail.TYPE = 3;
            if (mail.CONTENT.Length > 3950)  //字数不可超过4000
            {
                AlertMessage("Shall not exceed 4000 words");
                return;
            }
            if (Request.QueryString["id"] == null)
                num = mbo.Add(mail);
            else
                num = mbo.Update(mail);
            if (num > 0)
            {
                lbo.WriteLog(CurrentUser.Id, "Edit News");
                Response.Redirect("PPAPNews.aspx");
            }
        }
    }
}